import Cesium from '../../Ces/Cesium';
import GifPrimitive from './GifPrimitive';

function GifPolyline(options) {
  this._options = options;
  this._primitive = undefined;
  this._dirty = true;
  this._appearance = new Cesium.PolylineMaterialAppearance();
}

Object.defineProperties(GifPolyline.prototype, {
  show: {
    get() {
      return this._primitive || this._primitive.show;
    },
    set(value) {
      if (Cesium.defined(this._primitive)) {
        this._primitive.show = value;
      }
    },
  },
});

GifPolyline.prototype.updateGeometry = function (options) {
  if (Cesium.defined(options.width)) {
    this._options.width = options.width;
    this._dirty = true;
  }
  if (Cesium.defined(options.positions)) {
    this._options.positions = options.positions;
    this._dirty = true;
  }
};

GifPolyline.prototype.update = function (frameState) {
  if (this._dirty) {
    this._dirty = false;

    const geometry = new Cesium.PolylineGeometry({
      positions: this._options.positions,
      width: this._options.width,
      vertexFormat: Cesium.PolylineMaterialAppearance.POSITION_AND_ST,
    });

    const geometryInstances = [];

    geometryInstances.push(new Cesium.GeometryInstance({
      geometry,
    }));

    const options = {
      geometryInstances,
    };

    if (Cesium.defined(this._primitive)) {
      this._primitive.updateGeometry(options);
    } else {
      options.url = this._options.url;
      this._primitive = new GifPrimitive(Object.assign(this._options, options));
    }
  }

  this._primitive.update(frameState);
};


GifPolyline.prototype.isDestroyed = function () {
  return false;
};

GifPolyline.prototype.destroy = function () {
  this._primitive = this._primitive && this._primitive.destroy();

  return Cesium.destroyObject(this);
};


export default GifPolyline;
